package com.caiju.health.service.impl;

import com.caiju.health.entity.PageResult;
import com.caiju.health.mapper.ICheckGroupMapper;
import com.caiju.health.mapper.ISetMealMapper;
import com.caiju.health.pojo.CheckGroup;
import com.caiju.health.pojo.Setmeal;
import com.caiju.health.service.ICheackGroupService;
import com.caiju.health.service.ISetMealService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class SetMealServiceImpl implements ISetMealService {
    @Resource
    private ISetMealMapper setMealMapper;
    @Override
    public PageResult findSetMealByCondition(Integer currentPage, Integer pageSize, String queryString) {
        if (currentPage==null){
            currentPage = 1;
        }
        if (pageSize==null){
            pageSize = 5;
        }
        Page<Object> page = PageHelper.startPage(currentPage,pageSize);
//        queryString="null" ,误以为有值
        if(queryString.equals("null")){
            queryString = null;
        }else {
            queryString = "%" + queryString + "%";
        }
        List<Setmeal> checkItemList = setMealMapper.findCondition(queryString);
        PageResult pageResult = new PageResult(page.getTotal(),checkItemList);
        return  pageResult;
    }

    @Override
    public void deleteSetmeal(Integer id) {
        setMealMapper.updateSetmealByStatus(id);

    }

    @Override
    public void addSetmeal(Setmeal setmeal, Integer[] checkGroupIds) {
        setMealMapper.addSetmeal(setmeal);
        Integer setmealId = setmeal.getId();

        for (Integer checkGroupId : checkGroupIds) {
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("setmealId",setmealId);
            hashMap.put("checkGroupId",checkGroupId);
            setMealMapper.addSetmealAndCheckGroup(hashMap);
        }

    }

    @Override
    public Setmeal findSetmealById(Integer id) {
       Setmeal setmeal = setMealMapper.findSetmealById(id);
        return setmeal;
    }

    @Override
    public Integer[] findCheckGroupIdsById(Integer id) {
        Integer[] setmealIds = setMealMapper.findCheckGroupIdsById(id);

        return setmealIds;
    }

    @Override
    public void updateSetmeal(Setmeal setmeal) {
        setMealMapper.updateSetmeal(setmeal);
    }

    @Override
    public void deleteSetmealIdsById(Integer id) {
        setMealMapper.deleteSetmealIdsById(id);
    }

    @Override
    public void addSetmeal_CheckGroupbyId(Integer id, Integer[] checkGroupIds) {

        for (Integer checkGroupId:checkGroupIds){
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("setmealId",id);
            hashMap.put("checkgroupId",checkGroupId);
            setMealMapper.addSetmeal_CheckGroupbyId(hashMap);

        }
    }


}
